package com.wxy.ej.flow.modules.act.listen;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.TaskListener;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Slf4j
@Component("bindAnswerWfTaskListener")
@Scope("prototype")
public class BindAnswerWfTaskListener implements TaskListener {

    private Expression noticeType;

    @Override
    public void notify(DelegateTask delegateTask) {



        if ("assignment".equals(delegateTask.getEventName())) {
            log.info("---->act listener: BindAnswerWfTaskListener");

            /*int noticeTypeInt = 1;
            if (!StringUtils.isNull(noticeType)){
                String noticeTypeStr = noticeType.getValue(delegateTask).toString();
                try{
                    noticeTypeInt = Integer.parseInt(noticeTypeStr);
                }catch (Exception e){
                    //
                }
            }


            Map user = (Map)delegateTask.getVariable("user");
            String userId = (String)user.get("userId");
            if (userId.equalsIgnoreCase("-1")) {
                return;
            }
            String answerId = (String)user.get("answerId");

            String wfTaskId = delegateTask.getId();
            System.out.println("=========》BindAnswerWfTaskListener: 绑定用户Answer与WfTask：answerId："+answerId+"   wfTaskId:"+wfTaskId);

            // noticeType: 1=sms+push, 2=push, 3=sms
            cityTaskAutoService.bindAnswerWfTaskId(answerId, wfTaskId, noticeTypeInt);

            // 流程执行日志
            FlowElement flowElement = delegateTask.getExecution().getCurrentFlowElement();
            // ${companyId}
            String doc = ExpressionUtils.parse(flowElement.getDocumentation(), delegateTask);

            CtcTaskLogBizDto taskLogDto = new CtcTaskLogBizDto();
            taskLogDto.setCtcTaskId(Long.parseLong(delegateTask.getExecution().getProcessInstanceBusinessKey()));
            taskLogDto.setFlowElementId(flowElement.getId());
            taskLogDto.setFlowElementName(flowElement.getName());
            taskLogDto.setFlowElementDoc(doc);
            taskLogDto.setCreatedate(DateTool.currentDate("yyyy-MM-dd HH:mm:ss.SSS"));
            StringBuilder sb = new StringBuilder();
            sb.append("绑定用户处理结果表Answer与WfTask：answerId："+answerId+"   wfTaskId:"+wfTaskId);
            taskLogDto.setDescription(sb.toString());
            ctcTaskLogBizService.create(taskLogDto);

            // 如果是特保，就给城管发条信息*/


        }

    }

}
